package com.lpf.www.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lpf.www.common.dto.ResponseMsg;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author lpf
 * @Date 2021/12/31 上午9:21
 * @ProjectName live-streaming-os
 * @Version 1.0
 * @Description:
 */
public class DataHandler {
    public static boolean isEmpty(String data) {
        return data == null || "".equals(data.trim());
    }

    public static String getJsonString(Object responseMsg) {
        String res = "";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            res = objectMapper.writeValueAsString(responseMsg);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            System.out.println("转换为json字符串失败");
        }
        return res;
    }

    public static int StringToInt(String value) {
        int res = -1;
        try {
            res = Integer.parseInt(value);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("无效的转化（String to Integer）");
        }
        return res;
    }

    /**
     * 检查是否是邮箱
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {
        String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern compile = Pattern.compile(check);
        Matcher matcher = compile.matcher(email);
        return matcher.matches();
    }

    /**
     * 检查是否时电话
     * @param phone
     * @return
     */
    public static boolean isPhone(String phone){
        String check = "^(13[4,5,6,7,8,9]|15[0,8,9,1,7]|188|187)\\d{8}$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(phone);
        return matcher.matches();
    }

}
